

#Call requires here
#Requires to classes, modules, controllers, routers, and so. 
require 'TemplateSystem' 

class View 
  
  include TemplateSystem
  
	def initialize(mod)
		@subscriptor = nil
		@model = mod
		@innerHTML = ""
		@current = []
	end

	
	def addControlSubscriptor(sub)
		@subscriptor = sub
	end
	
  #Fetches the html, triggers template system
	def getCurrentHTML()   
    return(getTop('main','txt') << @innerHTML << getBottom('main','txt') )
	end
	
	#Denotes the start of an element in the html Example: ol , ul
	def startElement(elm)
	  @current.push( elm << ">" )
	  @innerHTML<< "\n" << "<" << elm
	end
	
	def closeElement
	  @innerHTML << "</" << @current.pop
	end
	
	#Adds an item of "type" with the specified content.
	def addItem(type,content)
	  @innerHTML << "<" + type + ">" << content << "</" + type + ">"
	end
	
  #Builds the table and appends it to the inner HTML
	def insertTable(hashedTable,listNames,headers)
		startElement("table")
		headers.each { |hd|
      addItem("th",hd) 
		}
		listNames.each{|candidate|
		  startElement("tr")
		  addItem("td",candidate)
		  addItem("td",hashedTable[candidate].to_s)
		  closeElement
		}
		closeElement
		
	end
	
	def makeshiftGraph(hashedTable,listNames)
	  num = 0
	  percentages = {}
	  listNames.each{|item|
	    num += hashedTable[item]
	  }
	  listNames.each{|item|
	    percentages[item] = (( Float(hashedTable[item]) / num) * 100).round
	  }
	  addItem("br","")
	  startElement("b")
	  listNames.each{|cnd|
      i = 0
      graph = '['
      while (i<percentages[cnd])
        graph << ":"
        i += 1
	    end
	    graph << "]" << "&nbsp;&nbsp;&nbsp;" << percentages[cnd].to_s << "%" << "&nbsp;" << cnd
	    addItem("p",graph)
    }
    closeElement
      
  end
  
  def massInsertTable(listHashes,names)
    startElement("center")
    listHashes.each{|hash|
      addItem("p",titleNames.pop)
      insertTable(hash,names)
    }
    closeElement
  end
	#No GetDocKey
	#No Clone
	#No OnClick
  
end
